package com.midongtech.news;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;


public class PreferencesUtils {

    public static final String TAG= "PreferencesUtils";


    public static final String DEFAULT_STRING = "";
    public static final int DEFAULT_INT = 0;

    private static volatile PreferencesUtils preferencesUtils;

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;


    private PreferencesUtils(Context context) {
        if (context != null) {
            sharedPreferences = context.getApplicationContext().getSharedPreferences("SP_TEST", 0);
            editor = sharedPreferences.edit();
        }
    }

    public static PreferencesUtils getInstance(Context context) {
        if (preferencesUtils == null) {
            synchronized (PreferencesUtils.class) {
                if (preferencesUtils == null) {
                    preferencesUtils = new PreferencesUtils(context.getApplicationContext());
                }
            }
        }
        return preferencesUtils;
    }


    public void putString(String key, String value) {
        if (editor != null) {
            if (TextUtils.isEmpty(value)) {
                editor.putString(key, "");
            } else {
                editor.putString(key, value);
            }
            editor.commit();
        }
    }

    public void putBoolean(String key, boolean value) {
        if (editor != null) {
            editor.putBoolean(key, value);
            editor.commit();
        }
    }

    public void removeKey(String key) {
        if (editor != null) {
            editor.remove(key);
            editor.commit();
        }
    }





    public String getString(String key, String defaultValue) {
        if (sharedPreferences != null) {
            String value = sharedPreferences.getString(key, "");
            if (TextUtils.isEmpty(value)) {
                return defaultValue;
            } else {
                return value;
            }
        } else {
            return defaultValue;
        }
    }

    public String getString(String key) {
        if (sharedPreferences != null) {
            String value = sharedPreferences.getString(key, DEFAULT_STRING);
            if (TextUtils.isEmpty(value)) {
                return value;
            } else {
                return value;
            }
        } else {
            return "";
        }
    }

    public boolean getBoolean(String key) {
        if (sharedPreferences != null) {
            return sharedPreferences.getBoolean(key, false);
        } else {
            return false;
        }
    }

    public boolean getBoolean(String key,boolean def) {
        if (sharedPreferences != null) {
            return sharedPreferences.getBoolean(key, def);
        } else {
            return false;
        }
    }

    public void putInt(String key, int value) {
        if (editor != null) {
            editor.putInt(key, value);
            editor.commit();
        }
    }

    public void putLong(String key, long value) {
        if (editor != null) {
            editor.putLong(key, value);
            editor.commit();
        }
    }

    public int getInt(String key, int defaultValue) {
        if (sharedPreferences != null) {
            return sharedPreferences.getInt(key, defaultValue);
        } else {
            return 0;
        }
    }

    public long getLong(String key) {
        if (sharedPreferences != null) {
            return sharedPreferences.getLong(key, 0);
        } else {
            return 0;
        }
    }

    public int getInt(String key) {
        if (sharedPreferences != null) {
            return sharedPreferences.getInt(key, DEFAULT_INT);
        } else {
            return 0;
        }
    }

    public void clear(Context context) {
        if (editor != null) {
            editor.clear().commit();
        }
    }


}
